# coding=utf-8
import psycopg2
from common.caseLog import Log

class Connect_PostgreSQL():
    def __init__(self, host, user, password, database, port):
        self.log = Log()
        try:
            self.db = psycopg2.connect(database=database,

                                       host=host,
                                       user=user,
                                       password=password,
                                       port=int(port)
                                       )
        except psycopg2.Error as e:
            self.log.error(e)

    def commit_table(self, sql):
        # 提交
        try:
            cursors = self.db.cursor()
            cursors.execute(sql)
            self.db.commit()
        except psycopg2.Error as e:
            self.log.error(e)
        finally:
            self.db.close()


    def select_table(self, sql):
        # 查询
        try:
            cursors = self.db.cursor()
            cursors.execute(sql)
            result = cursors.fetchall()
            return result
        except psycopg2.Error as e:
            self.log.error(e)
        finally:
            self.db.close()


if __name__ == '__main__':
    a = Connect_Mysql()
